﻿using CJiaBao.Core.Utils;
using Core.Configuration.Helper;
using Core.Configuration.Utils;
using Quartz;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Core.Quartz
{
    public class UrlJob : IJob
    {

        public async Task Execute(IJobExecutionContext context)
        {
            //Stopwatch stopwatch = new Stopwatch();
            //stopwatch.Restart();

            var json = context.MergedJobDataMap.GetString("UrlJobInfo")?.ToString();
            var jobInfo = JsonHelper.ToObject<UrlJobInfo>(json) ?? new UrlJobInfo();

            var status = await Start(jobInfo);

            //stopwatch.Stop();
        }

        public async Task<bool> Start(UrlJobInfo jobInfo)
        {
            try
            {
                Console.WriteLine($"执行计划: {jobInfo.Url}");
                await LogHelper.WriteAsync("quartz", $"执行计划: {jobInfo.Url}");

                await HttpHelper.GetAsync(jobInfo.Url);

                return true;
            }
            catch (Exception ex)
            {
                LogHelper.WriteAsync("quartz", $"执行异常: {ex.Message}");
            }
            return false;
        }
    }
}
